/**
 * Created by thinkjoy on 2017/7/17.
 */
var map,marker,infoWindow,geocoder;
var beginData,endData;
var bLo,eLo;
function init(){
    map = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 13
    });
    CloudMap();
    //显示交通情况
    var trafficLayer = new AMap.TileLayer.Traffic({
        zIndex: 15
    });
    trafficLayer.setMap(map);
    rightclick();
    showTopsInput('myaddress','1');
    showTopsInput('destination','2');
}

// 输入提示
function showTopsInput(id,data) {
    var content=['AMap.Autocomplete','AMap.PlaceSearch'];
    AMap.plugin(content,function(){
        var autoOptions = {
            city: "", //城市，默认全国
            input:id//使用联想输入的input的id
        };
        var placeSearch = new AMap.PlaceSearch({
            map:map
        });
        autocomplete= new AMap.Autocomplete(autoOptions);
        AMap.event.addListener(autocomplete, "select", function(e){
            // console.log("区域："+e.poi.district);
            getLocation(e,data);
            if(data=='1'){
                beginData=e;
                placeSearch.setCity(e.poi.adcode);
                placeSearch.search(e.poi.name);
            }
        });
    })
}

// 获取经纬度
function getLocation(data,flag) {
    var strs=new Array();
    var strs1=new Array();
    strs=data.poi.district.split("市");
    if(strs[0].indexOf('区')>0){
        strs1=strs[0].split("区");
    }else if(strs[0].indexOf('省')>0){
        strs1=strs[0].split("省");
    }else{
        strs1[1]=strs[0];
    }
    AMap.service('AMap.Geocoder', function () {//回调函数
        geocoder = new AMap.Geocoder({
            city: strs1[1]
        });
        geocoder.getLocation(data.poi.name, function (status, result) {
            if (status === 'complete' && result.info === 'OK') {
                // console.log("详细地址:"+result.geocodes[0].formattedAddress);
                // console.log('经纬度：' + result.geocodes[0].location+"  匹配级别："+result.geocodes[0].level);
                if(flag=='1'){
                    bLo=result.geocodes[0].location;
                }else{
                    map.clearMap();
                    eLo=result.geocodes[0].location;
                    var search, center = eLo;
                    var searchOptions = {
                        map: map,
                        keywords: '',
                        pageSize: 0
                    };
                    //加载CloudDataSearch服务插件
                    AMap.service(["AMap.CloudDataSearch"], function() {
                        search = new AMap.CloudDataSearch('596eb6ebafdf521e86bc3b50', searchOptions);
                        search.searchNearBy(center, 1000,function (status,result) {
                            if(status=='complete'){
                                for(var i=0;i<result.datas.length;i++){
                                    showMaker(result.datas[i]);
                                }
                            }else if(status=='no_data'){
                                alert('该区域暂无租房信息，请重新查询');
                            }
                        });
                        search.clear();
                    });
                }
            } else {
                console.log('错误:' + result.info);
            }
        });
    });
}

// map.clearMap();
// document.getElementById('panel').innerHTML='';

// 显示点标注
function showMaker(data) {
    marker=new AMap.Marker({
        icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
        position:data._location,
        animation:'AMAP_ANIMATION_BOUNCE'
    });
    //鼠标点击marker弹出自定义的信息窗体
    AMap.event.addListener(marker, 'click', function() {
        openInfo(data);
    });
    marker.setMap(map);
}

// 信息窗体
function openInfo(clouddata) {
    var infoWindow = new AMap.InfoWindow({
        content: "<h3><font face='微软雅黑' color='#36F'>"+clouddata._name +
            "<a target='_blank' href='/front/myCenter/houseInfo/frontHouseList?cloudId="+clouddata._id+"'><font color='#01AAED' face='微软雅黑' style='margin-left: 10px;'>进入>></font></a>"+
            "</font></h3><hr>地址：" + clouddata._address+"<br><br>" +
            "<a class='layui-btn layui-btn-small' href='javascript:DriverDaoHang()'>驾车</a> " +
            "<a class='layui-btn layui-btn-small' href='javascript:TransferDaoHang()'>公交</a>" +
            "<a class='layui-btn layui-btn-small' href='javascript:WalkingDaoHang()'>步行</a>",
        size: new AMap.Size(300, 0),
        autoMove: true,
        offset: new AMap.Pixel(0, -25)
    });
    endData=clouddata._address;
    eLo=clouddata._location;
    infoWindow.open(map, clouddata._location);
}

// 驾车导航
function DriverDaoHang() {
    AMap.service('AMap.Driving',function(){//回调函数
        //构造路线导航类
        var driving= new AMap.Driving({
            map: map,
            panel: 'panel'
        });
        driving.search(bLo,eLo);
    });
}

// 公交导航
function TransferDaoHang() {
    AMap.service('AMap.Transfer',function(){//回调函数
        //构造公交路线导航类
        var bstrs=new Array();
        var bstrs1=new Array();
        var estrs=new Array();
        var estrs1=new Array();
        var bcity,ecity;
        bstrs=beginData.poi.district.split("市");
        if(bstrs[0].indexOf('区')>0){
            bstrs1=bstrs[0].split("区");
        }else if(bstrs[0].indexOf('省')>0){
            bstrs1=bstrs[0].split("省");
        }else {
            bstrs1[1]=bstrs[0];
        }

        estrs=endData.split("市");
        if(estrs[0].indexOf('区')>0){
            estrs1=estrs[0].split("区");
        }else if(estrs[0].indexOf('省')>0){
            estrs1=estrs[0].split("省");
        }else{
            estrs1[1]=estrs[0];
        }
        bcity=bstrs1[1]; ecity=estrs1[1];
        var transfer= new AMap.Transfer({
            map:map,
            panel:'panel',
            city:bcity,
            cityd:ecity,
            policy: AMap.TransferPolicy.LEAST_TIME //乘车策略
        });
        transfer.search(bLo,eLo);
    })
}

// 步行导航
function WalkingDaoHang() {
    AMap.service('AMap.Walking',function(){//回调函数
        //实例化Walking
        var walking= new AMap.Walking({
            map:map,
            panel:'panel'
        });
        walking.search(bLo,eLo);
    })
}

// 右键
function rightclick() {
    var contextMenu = new AMap.ContextMenu();  //创建右键菜单
    //右键放大
    contextMenu.addItem("放大一级", function() {
        map.zoomIn();
    }, 0);
    //右键缩小
    contextMenu.addItem("缩小一级", function() {
        map.zoomOut();
    }, 1);
    //右键显示全国范围
    contextMenu.addItem("缩放至全国范围", function(e) {
        map.setZoomAndCenter(4, [108.946609, 34.262324]);
    }, 2);
    //地图绑定鼠标右击事件——弹出右键菜单
    map.on('rightclick', function(e) {
        contextMenu.open(map, e.lnglat);
    });
}

// 云图
function CloudMap() {
    AMap.plugin('AMap.CloudDataLayer', function() {
        var layerOptions = {
            clickable: true
        };
        var cloudDataLayer = new AMap.CloudDataLayer('596eb6ebafdf521e86bc3b50', layerOptions);
        cloudDataLayer.setMap(map);//添加到地图
        AMap.event.addListener(cloudDataLayer, 'click', function(result) {
            var clouddata = result.data;
            openInfo(clouddata);
        });
    });
}

